package com.sheep.taobao;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.json.JSONArray;
import org.json.JSONObject;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.ItemGetRequest;
import com.taobao.api.response.ItemGetResponse;

public class ProductDetail {

	protected static String sandboxUrl = "http://gw.api.tbsandbox.com/router/rest";// 沙箱环境调用地址
	protected static String url = "http://gw.api.taobao.com/router/rest";//正式环境调用地址
	protected static String appkey = "21579332";
	protected static String appSecret= "497baa9189decdc0468bd7a933a9ce8c";
	protected static String sessionKey= "6100319bb710450501a2eeabdde1c1357b01dab4e77f87495261480";
	protected static String sandboxAppkey = "1021579332";
	protected static String sandboxSecret  = "sandbox189decdc0468bd7a933a9ce8c";
	protected static String sandboxSessionkey = "6100e24058bf13e2329a46dae51eb3a21a5b249c28c210795261480"; 
	
	public static void main(String[] args)  throws ApiException, DocumentException {
		//String detail = getProductDetail(27192712171L);
		//String detail = getProductDetail(16397137242L);
		//System.out.println(detail);
		//handleXml(detail);
		//Product p = handleJSON( detail );
		GetDataUtils.getDatasFromTaoKe("羊绒", "");
		
		//System.out.println(p);
	}
	
	public static String getProductDetail(Long productId) throws ApiException {
		TaobaoClient client=new DefaultTaobaoClient(url, appkey, appSecret, "json");
		ItemGetRequest req=new ItemGetRequest();
		//req.setFields("detail_url,num_iid,title,nick,type,cid,seller_cids,props,input_pids,input_str,desc,pic_url,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,approve_status,postage_id,product_id,auction_point,property_alias,item_img,prop_img,sku,video,outer_id,is_virtual");
		req.setFields("price,item_img,property_alias,props,auction_point");
		req.setNumIid(productId);
		ItemGetResponse response = client.execute(req , sessionKey);
		String detail = response.getBody();
		return detail;
	}
	
	//public static 
	
	public static Product handleJSON(String json) {
		Product product = new Product();
		JSONObject jsonObj = new JSONObject(json);
		//jsonObj.getJSONArray("item_imgs");
		//jsonObj.get("jsonObj");
		//jsonObj.getString("jsonObj");
		JSONObject rootObj = (JSONObject)jsonObj.get("item_get_response");
		JSONObject item = (JSONObject)rootObj.get("item");
		String price = (String)item.get("price");
		product.setPrice(Float.valueOf(price));
		JSONObject imageItems = (JSONObject)item.get("item_imgs");
		JSONArray imageItem = (JSONArray)imageItems.get("item_img");
		String[] imagesUrl = new String[imageItem.length()];
		for(int i=0; i<imageItem.length(); i++) {
			String str = (String)(imageItem.getJSONObject(i).get("url"));
			imagesUrl[i] = str;
		}
		product.setImagesUrl(imagesUrl);
		String aliasItems = (String)item.get("property_alias");
		String[] propertiesName = aliasItems.split(";");
		product.setPropertiesName(propertiesName);
		return product;
	}
	
	public static void  handleXml(String xml) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document doc = DocumentHelper.parseText(xml);
		Element el = doc.getRootElement();
		System.out.println(el);
	}
}
